﻿//Copyright (c) 2012 The cwkshell Authors. All rights reserved. 
// Use of this source code is governed by a BSD-style license that can be 
// found in the LICENSE file. 
#include "cuc/src/cwkshell/view/shell_browser_container.h" 

#include "base/auto_reset.h"
#include "base/win/wrapped_window_proc.h"
#include "content/public/browser/web_contents.h"
#include "cuc/src/cwkshell/browser/shell_browser.h" 

#include "cuc/src/cwkshell/view/shell_browser_container_non_client_view.h" 
#include "ui/gfx/rect.h"
#include "ui/base/win/hwnd_util.h"

namespace{

	const int KBorderSize =2;
}
namespace cuc{ 
	namespace shell{ 



		ShellBrowserContainer::ShellBrowserContainer(content::WebContents* web_contents)
			: ShellWindow(web_contents),
			  KTitleButtonHeight( 
			  cuc::shell::ShellBrowser::property()->HasTitleButton()
			  ?cuc::shell::ShellBrowser::property()->GetTitleButtonHeight():
		cuc::shell::ShellBrowser::property()->GetTitleBarHeight())
		{

			Init();
			if(ShellBrowser::property()->IsFullScreen()){
				AddAccelerator(ui::Accelerator(ui::VKEY_F12, ui::EF_NONE));
			}
			window_title_ = L"cwkshell_container";

		}

		ShellBrowserContainer::~ShellBrowserContainer() {

			ShellBrowser::instance()->CloseAllWindows();
			ShellBrowser::instance()->Quit();

		}

		ShellBrowserContainer* 
			ShellBrowserContainer::CreatShellBrowserContainer(
			content::WebContents* web_contents) {
			
			LOG(INFO)<<"ShellBrowserContainer::ShellBrowserContainer CALLED";
			ShellBrowserContainer* shell = new ShellBrowserContainer(web_contents);	
				bool ismaxsize = ShellBrowser::property()->IsMaxsize();
			if(ShellBrowser::property()->IsFullScreen()){
				shell->ShowWindowFullScreen(gfx::Size(800,600));
			}
			else
				shell->ShowWindow(gfx::Size(800,600),ismaxsize);
			LOG(INFO)<<"ShellBrowserContainer::CreatShellBrowserContainer() DONE";
			return shell;
		}

		views::NonClientFrameView* 
			ShellBrowserContainer::CreateNonClientFrameView( views::Widget* widget ) 
		{

			ShellBrowserContainerNonClientView* view = new ShellBrowserContainerNonClientView;
			view->Init(widget);

			LOG(INFO)<<"ShellBrowserContainer::CreateNonClientFrameView DONE";
			return view;
		}

		string16 ShellBrowserContainer::GetWindowTitle() const 
		{
			return ShellBrowser::instance()->GetShellProperty()->GetAppName();
		}

		void ShellBrowserContainer::ResizeSubViews()
		{
			RECT rc;
			GetClientRect(window_, &rc);
			int x ,y,width,height;		
			if(this->GetWidget()->IsFullscreen()){
				x = 0;
				y = 0;	
				width =  rc.right;
				height = rc.bottom;
			}
			else if(this->GetWidget()->IsMaximized()){
				x = 0;
				y = KTitleButtonHeight;	
				width =  rc.right;
				height = rc.bottom-KTitleButtonHeight;
			}
			else{
				x = KBorderSize;
				y = KTitleButtonHeight;
				width =  rc.right-2*KBorderSize;
				height = rc.bottom-KTitleButtonHeight-KBorderSize;
			}
			MoveWindow(GetContentView(), x, y, width,height, TRUE);
		}

		


	} //namespace shell 
} //namespace cuc 
